//
//  Connector.m
//  cliqset
//
//  Created by David Hinson on 3/5/09.
//  Copyright 2009 Sumner Systems Management, Inc.. All rights reserved.
//

#import "Connector.h"
#import "Cliqset.h"

@implementation Connector

@synthesize mode          = _mode;
@synthesize receivedData  = _receivedData;
@synthesize theRequest    = _theRequest;
@synthesize theConnection = _theConnection;
@synthesize parent        = _parent;

-(id)init {
	self = [super init];
	return self;
}

// Did receive response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [_receivedData setLength:0];
}

// Did receive data
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_receivedData appendData:data];
}

// Did finish loading
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	
	Cliqset * cliqset = (Cliqset*)self.parent;
	
	if (cliqset) {[cliqset processMessage:self.receivedData mode:self.mode];}
	
	[_receivedData release];
	[_theConnection release];
}

// Did fail with error
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    [_receivedData release];
    [_theConnection release];
	
	//NSString *errorMessage = [@"" stringByAppendingFormat:@"Network connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]];	
    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);		
	//[myApp addTextToLog:errorMessage withCaption:@"cliqset"];
	
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

-(void)dealloc {
	[_parent release];
	[_theConnection release];
	[_theRequest release];
	[_receivedData release];
	[super dealloc];
}

@end
